<?php

/*
	* OpenGSP (c) 2011
	* Author(s): Christopher Martino
	* Module: accounts.base.php
	* Updated: 29/03/2011
	* http://opengsp.com/
*/

class Accounts_BASE
{
	public static $account_session;
	
	
	function __construct(/* NULL Parameters */)
	{
		if(!isset($_SESSION['OpenGSP']))
		{
			self::$account_session = SESSION_UNSET;
		}
		else self::$account_session = $_SESSION['OpenGSP'];
	}
	
	
	
	public static function create_account($post_data)
	{
		if(strlen($post_data['Account_Username']) < 5) return INVALID_USER_STRLEN;
		if(strlen($post_data['Account_Password']) < 5) return INVALID_PASS_STRLEN;
		if(strlen($post_data['Account_EmailADD']) < 5) return INVALID_MAIL_STRLEN;
		if(strlen($post_data['Account_ClientNM']) < 5) return INVALID_NAME_STRLEN;
		
		$password_encryption = sha1(md5($post_data['Account_Password']));
	
		return MySQLi_BASE::query_database("INSERT INTO OpenGSP_Accounts (Account_Username, Account_Password, Account_Email, Account_Client) VALUES('".MySQLi_BASE::escape_input($post_data['Account_Username'])."', '".$password_encryption."', '".MySQLi_BASE::escape_input($post_data['Account_EmailADD'])."', '".MySQLi_BASE::escape_input($post_data['Account_ClientNM'])."')");
	}
	
	
	
	public static function create_session($post_data)
	{
		$password_encryption = sha1(md5($post_data['Account_Password']));
		
		$logical_test = MySQLi_BASE::total_rows("SELECT * FROM OpenGSP_Accounts WHERE (Account_Username = '".MySQLi_BASE::escape_input($post_data['Account_Username'])."' OR Account_Email = '".MySQLi_BASE::escape_input($post_data['Account_Username'])."') AND Account_Password = '".$password_encryption."'");
		
		if($logical_test == 1)
		{
			$account_data = MySQLi_BASE::fetch_data("SELECT * FROM OpenGSP_Accounts WHERE (Account_Username = '".MySQLi_BASE::escape_input($post_data['Account_Username'])."' OR Account_Email = '".MySQLi_BASE::escape_input($post_data['Account_Username'])."') AND Account_Password = '".$password_encryption."'");
			
			$_SESSION['OpenGSP'] = $account_data['Account_ID'];
			
			return SUCCESSFUL_LOGIN;
		}
		else return INVALID_LOGIN;
	}
	
	
	
	public static function update_account_balance($account_id, $value)
	{
		$account_array = MySQLi_BASE::fetch_data("SELECT * FROM OpenGSP_Accounts WHERE Account_ID = '".MySQLi_BASE::escape_input($account_id)."'");
		
		eval('$new_balance = '.$account_array['Account_Balance'].$value);
		
		return MySQLi_BASE::query_database("UPDATE OpenGSP_Accounts SET Account_Balance = '".$new_balance."' WHERE Account_ID = '".$account_array['Account_ID']."'");
	}
}
?>